Skip to content

Fire onDidChangeEnvironment when a refresh auto-selects a project venv - #2048

Open
AndreasReichel wants to merge 1 commit into
microsoft:mainfrom
AndreasReichel:fix-2046
Open

AndreasReichel wants to merge 1 commit into
microsoft:mainfrom
AndreasReichel:fix-2046

Conversation

@AndreasReichel

Copy link
Copy Markdown

Fixes #2046

Cause

BuiltInEnvironmentManager.loadEnvMap() runs after every refresh of the venv manager and maps each project to an environment:

  • If the project has a persisted venv, it maps the project to it and fires onDidChangeEnvironment when the environment differs.
  • If not, it picks the project's own venv from the discovered environments and only calls fsPathToEnv.set().

Until then, get() returns the global fallback for such a project. When a venv is created in the folder while VS Code runs, the next refresh maps the project to it: getEnvironment() switches to the venv, but no manager event fires. Because refreshEnvironment() only runs on a manager event, the public onDidChangeEnvironment never fires either. Consumers that cache the interpreter keep the old one; Pylint, for example, keeps linting with the global Python until it is restarted.

Fix

In the second case, fire onDidChangeEnvironment when the picked environment differs from the project's previous entry, or from the global fallback if there was none. The map is cleared at the start of loadEnvMap(), so its previous entries are copied first. refreshEnvironment() still drops unchanged environments, so refreshes that find the same venv stay quiet.

Tests

Two unit tests in venvManager.autoSelect.unit.test.ts:

  • A refresh that finds a newly created venv in the project fires the event. This test fails without the fix.
  • A refresh that finds the same venv again fires nothing.

Also checked by hand with a local build of 1.44.0 plus this change. I opened a folder with only main.py, ran python -m venv .venv in it, and compared the logs:

Without the fix With the fix
onDidChangeEnvironment after .venv is discovered none fires, Python 3.14 -> .venv
Pylint 2026.8.0 stays on the global Python until "Pylint: Restart Server" restarts on .venv immediately

Same result with python.useEnvironmentsExtension unset, with it set to true, and with python-envs.defaultEnvManager set to ms-python.python:venv.

🤖 Generated with Claude Code

When a project has no persisted venv selection, get() returns the global
fallback until a refresh finds a venv in the project, e.g. one created
after VS Code started. loadEnvMap() then mapped the project to that venv
without firing onDidChangeEnvironment, so consumers that cache the
interpreter (e.g. Pylint) kept the old one.

Fixes microsoft#2046

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AndreasReichel

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Vector Informatik GmbH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getEnvironment() switches to a newly created venv without firing onDidChangeEnvironment

1 participant